package com.epfresh.supplier.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.epfresh.api.global.BaseFragment;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.AgentMoneyDetailActivity;
import com.epfresh.supplier.entity.BaseStoreInfo;

/**
 * @author wangzhanghuan
 * @Description
 * @version 1.0
 */
public class BaseStoreInfoFragment extends BaseFragment {

    private TextView mTvStoreName;

    private TextView mTvAddress;
    private TextView mTvPhone;
    private View llPhone;

    private TextView mTvCertStatus;
    private TextView mTvType;
    private TextView mTvApplyDetail;
    
    ImageView ivRate;
    TextView tvApplyDetailTitle;



    private BaseStoreInfo mBaseStoreInfo;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_base_store_info, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        initView(view);
      
    }

    public void setBaseStoreInfo(BaseStoreInfo baseStoreInfo){
        mBaseStoreInfo = baseStoreInfo;
    }

    private void initView( View v) {


        mTvStoreName =  v.findViewById(R.id.tv_store_name);
        mTvAddress =  v.findViewById(R.id.tv_address);
        mTvPhone =  v.findViewById(R.id.tv_phone);
        llPhone = v.findViewById(R.id.ll_phone);
        llPhone.setOnClickListener(this);
        mTvCertStatus = v.findViewById(R.id.tv_cert_status);
        mTvCertStatus =  v.findViewById(R.id.tv_cert_status);
        mTvApplyDetail =  v.findViewById(R.id.tv_apply_detail);
        mTvType =  v.findViewById(R.id.tv_type);

        ivRate = v.findViewById(R.id.iv_rate);
        tvApplyDetailTitle = v.findViewById(R.id.tv_check);

        mTvApplyDetail.setOnClickListener(this);

        updateView(mBaseStoreInfo);

    }

    public void updateAgencyDetail(boolean isShow){
        if (isShow){
            mTvApplyDetail.setVisibility(View.VISIBLE);
            ivRate.setVisibility(View.VISIBLE);
            tvApplyDetailTitle.setVisibility(View.VISIBLE);
            
        }else {
            mTvApplyDetail.setVisibility(View.GONE);
            ivRate.setVisibility(View.GONE);
            tvApplyDetailTitle.setVisibility(View.GONE);

        }
    }

    public void updateView(BaseStoreInfo baseStoreInfo){
        if(getActivity() == null || mTvStoreName == null){
            return;
        }
        if (baseStoreInfo != null) {

            mTvStoreName.setText(baseStoreInfo.getName());
            mTvAddress.setText(baseStoreInfo.getAddress());
            mTvPhone.setText(baseStoreInfo.getManagerName() +"   "+baseStoreInfo.getManagerPhone());
            mTvType.setText(baseStoreInfo.getSubjectType());
            String agentStatus = baseStoreInfo.getAgentStatus();
            if ("2".equals(agentStatus)){
                updateAgencyDetail(true);
            }else {
                updateAgencyDetail(false);
            }

        }else {
            mTvStoreName.setText("");
            mTvAddress.setText("");
            mTvPhone.setText("");
            mTvType.setText("");
            updateAgencyDetail(false);
        }
    }



    @Override
    public String getBaseTag() {
        return "";
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_phone:
                Intent call;
                if(mBaseStoreInfo != null)
                    call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mBaseStoreInfo.getManagerPhone()));//调出来拨打页面；
                else
                    call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"));//调出来拨打页面；
                getContext().startActivity(call);
                break;
            case R.id.tv_apply_detail:
                if (mBaseStoreInfo != null) {
                    Intent intent = new Intent(getContext(), AgentMoneyDetailActivity.class);
                    intent.putExtra("baseStoreId", mBaseStoreInfo.getId());
                    startActivity(intent);
                }
                break;
        }
    }



}
